@tool
class_name Skill_Control_Bind
extends SkillControl

## Bind 绑定
## 主要Action和次要Action一起执行，可以在主要Action结束时Stop此节点

#region 属性
## 是否在主要Action结束时结束此节点
@export var is_stop_by_primary_action_finished:bool = true
@export var primary_action:SkillAction
var cur_finished_sub_action_amount:int = 0
var is_primary_finished:bool = false
var primary_action_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			if not connect_obj_dict.has(primary_action.skill_action_release_completed):
				connect_obj_dict.set(primary_action.skill_action_release_completed,ConnectObj.new(primary_action.skill_action_release_completed,[]))
			var connect_obj:ConnectObj = connect_obj_dict.get(primary_action.skill_action_release_completed) as ConnectObj
			connect_obj.connect_single(on_sub_action_release_completed)
			if not connect_obj_dict.has(primary_action.skill_action_interrupted):
				connect_obj_dict.set(primary_action.skill_action_interrupted,ConnectObj.new(primary_action.skill_action_interrupted,[]))
			connect_obj = connect_obj_dict.get(primary_action.skill_action_interrupted) as ConnectObj
			connect_obj.connect_single(on_sub_action_interrupted)
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.release(args)
		for action in sub_actions:
			action.release(args)
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.interrupt()
		for action in sub_actions:
			action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.complete()
		for action in sub_actions:
			action.complete()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.cool()
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.cool_complete()
		for action in sub_actions:
			action.cool_complete()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.reset_cool()
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if primary_action:
			primary_action.destroy()
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	return primary_action.check_can_release()
## 状态转换后的处理
func after_state_change(from_state:GameEnum.SkillActionState, to_state:GameEnum.SkillActionState):
	super.after_state_change(from_state,to_state)
	match to_state:
		GameEnum.SkillActionState.Waiting:
			cur_finished_sub_action_amount = 0
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if not is_releasing:
		return
	if skill_action != primary_action:
		cur_finished_sub_action_amount += 1
	else:
		is_primary_finished = true
		if is_stop_by_primary_action_finished:
			is_completed = true
	if check_is_completed():
		complete(primary_action_result)
func on_sub_action_interrupted(skill_action:SkillAction):
	if not is_releasing:
		return
	if skill_action != primary_action:
		cur_finished_sub_action_amount += 1
	else:
		is_primary_finished = true
		if is_stop_by_primary_action_finished:
			is_completed = true
	if check_is_completed():
		complete(GameEnum.SkillReleaseResult.Interrupted)
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	if not primary_action:
		warnings.append("主要子Action不能为空")
	if primary_action.get_parent() != self:
		warnings.append("主要子Action必须是此节点的子节点")
	return warnings
#endregion
